﻿//Write a program to convert hexadecimal numbers to their decimal representation.


using System;
using System.Numerics;

public class HexadecimalToDecimal
{
    public static void Main()
    {
        Console.Write("Enter hexadecimal number to convert to decimal Num = ");
        string number = Console.ReadLine().ToUpper();
        char[] array = PopulateArray(number);
        BigInteger decimalNUmber = ConvertToDecimal(array);
        Console.Write("The decimal number is {0}\n", decimalNUmber);
    }

    public static BigInteger ConvertToDecimal(char[] array)
    {
        BigInteger decimalNumber = 0;
        for (int i = 0; i < array.Length; i++)
        {
            BigInteger multiply = 1;
            if ((array[i] - 48) != 0)
            {
                for (int j = 0; j < i; j++)
                {
                    multiply *= 16;
                }
            }
            decimalNumber += (array[i] - 48 > 9 ? (array[i] - 55) * multiply : (array[i] - 48) * multiply);
        }

        return decimalNumber;        
    }

    private static char[] PopulateArray(string str)
    {
        char[] array = new char[str.Length];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = str[array.Length - i - 1];
        }

        return array;
    }
}